home *** CD-ROM | disk | FTP | other *** search
/ ftp.cs.arizona.edu / ftp.cs.arizona.edu.tar / ftp.cs.arizona.edu / icon / newsgrp / group97a.txt / 000126_icon-group-sender _Sun May 18 12:31:04 1997.msg < prev    next >
Internet Message Format  |  2000-09-20  |  3KB

  1. Received: from kingfisher.CS.Arizona.EDU by cheltenham.cs.arizona.edu; Mon, 19 May 1997 09:55:21 MST
  2. Received: by kingfisher.CS.Arizona.EDU; (5.65v3.2/1.1.8.2/08Nov94-0446PM)
  3.     id AA07392; Mon, 19 May 1997 09:55:21 -0700
  4. Date: Sun, 18 May 1997 12:31:04 -0400
  5. Message-Id: <199705181631.MAA19857@axp.cmpu.net>
  6. Mime-Version: 1.0
  7. Content-Type: text/plain
  8. Content-Transfer-Encoding: 7bit
  9. From: gep2@computek.net
  10. Subject: Problem with Program
  11. To: icon-group@cs.arizona.edu
  12. X-Mailer: SPRY Mail Version: 04.00.06.17
  13. Errors-To: icon-group-errors@cs.arizona.edu
  14. Status: RO
  15. Content-Length: 2453
  16.  
  17. >Could someone tell me why the program below isn't working?  It's late and
  18. I must be overlooking something.  Given input like the following
  19.  
  20. >This is a sample line 1{S {S
  21. This is a sample line 2{A {A
  22. This is a sample line 3{O {O
  23.  
  24. >it should be produce the following output
  25. S: 2
  26. A: 2
  27. O: 2
  28.  
  29. >Thanks in advance.
  30.  
  31. ----------------
  32.  
  33. >procedure main()
  34. chars := &letters ++ &digits ++ '{.*[]?'     #valid characters
  35. noS := 0
  36. noA := 0
  37. noO := 0
  38. while line := read() do                   #read in line
  39.    {
  40.    line ?                              #textscan line
  41.       {
  42.       while tab(upto(chars)) do           #look for valid character
  43.          {
  44.          word := tab(many(chars))         #word = valid character through
  45. next invalid character
  46.          word ?                        #textscan word
  47.             {
  48.             while tab(upto("{") +1) do    #move just past bracket (will
  49. fail if there is none)
  50.                {
  51.                wordfrag := move(1)        #wordfrag = one character past bracket 
  52.                case wordfrag of        #if wordfrag is
  53.                   {
  54.                   "S"   :  noS +:= 1      #S, then increment noS by 1
  55.                   "A"   :  noA +:= 1      #A, then increment noA by 1
  56.                   "O"   :  noO +:= 1      #O, then increment noO by 1
  57.                   }
  58.                }
  59.             }
  60.          }
  61.       }
  62.    }
  63. write( "S: " || noS || \n || "A: " || noA || \n || "O: " || noO )
  64. end
  65.  
  66. To start with, you're writing this in what seems like a terribly convoluted way, 
  67. and not letting yourself take advantage of the kinds of things that set 
  68. Icon/S*BOL apart from other languages (notably, tables).  Secondly, I'd look for 
  69. the "{" character followed by something, and then use that something to 
  70. increment a counter in a table.  This prevents you having to hardwire in just 
  71. three key letters.
  72.  
  73. For instance, if I were going to write the same program in S*BOL, it might look 
  74. like:
  75.  
  76.            t = table()
  77.            pat = fence breakx("{") "{" notany(" ") . key
  78. readrec    line = input                               :f(donerd)
  79. countlp    line pat =                                 :f(readrec)
  80.            t[key] = t[key] + 1                        :(countlp)
  81. donerd     a = convert(t,"ARRAY")                     :f(end)
  82. outlp      output = a[(i = i + 1), 1] "  " a[i,2]     :s(outlp)
  83. end  
  84.  
  85. Gordon Peterson
  86. http://www.computek.net/public/gep2/
  87. Support the Anti-SPAM Amendment!  Join at http://www.cauce.org/
  88.  
  89.